import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      meta: { title: '登录' },
      component: () => import('../views/LoginPage.vue'),
    },
    {
      path: '/login',
      // 路由重定向
      redirect: '/'
    },
    {
      path: '/Home',
      meta: { title: '首页' },
      component: () => import('../views/HomePage.vue'),
      children: [
        {
          path: '/user',
          meta: { title: '用户列表' },
          component: () => import('../views/user/userList.vue'),
        },
        {
          path: '/role',
          meta: { title: '角色列表' },
          component: () => import('../views/role/roleList.vue'),
        },
        {
          path: '/dept',
          meta: { title: '部门列表' },
          component: () => import('../views/dept/deptList.vue'),
        }
      ]
    },
  ],
})

//路由守卫，在导航完成后触发
router.afterEach((to, from) => {
  if (to.meta && to.meta.title) {
    //每次路由导航完成后动态修改页面标题
    document.title = to.meta.title
  }
})

export default router
